package edu.wtbu.cs.bishe.controller;


import edu.wtbu.cs.bishe.service.EmailService;
import edu.wtbu.cs.bishe.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

// 5. 控制器层
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final UserService userService;
    private final EmailService emailService;

    /**
     * 邮箱发送验证码
     * @param email
     * @param code
     * @return
     */
    @GetMapping("/test-send-code")
    public ResponseEntity<?> testSendVerificationCode(@RequestParam String email,
                                                      @RequestParam String code) {
        emailService.sendVerificationCode(email,code);
        return ResponseEntity.ok("验证码已发送");
    }

    // 发送验证码（带频率限制）
    @PostMapping("/send-code")
    public ResponseEntity<?> sendVerificationCode(@RequestParam String email) {
        userService.sendVerificationCode(email);
        return ResponseEntity.ok("验证码已发送");
    }

    // 验证码验证
    @PostMapping("/verify-code")
    public ResponseEntity<?> verifyCode(
            @RequestParam String email,
            @RequestParam String code
    ) {
        userService.verifyCode(email, code);
        return ResponseEntity.ok("邮箱验证成功");
    }

    // 完成注册（设置密码）
    @PostMapping("/complete-registration")
    public ResponseEntity<?> completeRegistration(
            @RequestParam String email,
            @RequestParam String password
    ) {
        userService.completeRegistration(email, password);
        return ResponseEntity.ok("注册完成");
    }
}
